// plant
let animationChomper = {
    name: 'chomper',
    pathFormat: 'img/Plants/chomper/[action]/chomper_[action]_[index].png',
    actions: [
        // {
        //     name: 'idle',
        //     numberOfFrames: 13,
        // },
        {
            name: 'attack',
            numberOfFrames: 9,
        },
        {
            name: 'digest',
            numberOfFrames: 6,
        },
    ]
}

let animationFumeshroom = {
    name: 'fumeshroom',
    pathFormat: 'img/Plants/fumeshroom/[action]/fumeshroom_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 16,
        },
        {
            name: 'attack',
            numberOfFrames: 1,
        },
        {
            name: 'sleep',
            numberOfFrames: 14,
        },
    ]
}

let animationGarlic = {
    name: 'garlic',
    pathFormat: 'img/Plants/garlic/[action]/garlic_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 12,
        },
        {
            name: 'body1',
            numberOfFrames: 1,
        },
        {
            name: 'body2',
            numberOfFrames: 1,
        },
    ]
}

let animationGatlingpea = {
    name: 'gatlingpea',
    pathFormat: 'img/Plants/gatlingpea/[action]/gatlingpea_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 13,
        },
    ]
}

let animationGloomshroom = {
    name: 'gloomdshroom',
    pathFormat: 'img/Plants/gloomdshroom/[action]/gloomdshroom_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 12,
        },
        {
            name: 'sleep',
            numberOfFrames: 13,
        },
        {
            name: 'attack',
            numberOfFrames: 2,
        },
    ]
}

// file:///D:/Code/plants_vs_zombies/img/Plants/gloomshroom/attack/gloomshroom_attack_1.png
// file:///D:/Code/plants_vs_zombies/img/Plants/gloomshroom/attack/gloomdshroom_attack_1.png
let animationHypnoshroom = {
    name: 'hypnoshroom',
    pathFormat: 'img/Plants/hypnoshroom/[action]/hypnoshroom_[action]_[index].png',
    actions: [
        {
            name: 'sleep',
            numberOfFrames: 15,
        },
    ]
}

let animationIceshroom = {
    name: 'Iceshroom',
    pathFormat: 'img/Plants/Iceshroom/[action]/Iceshroom_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 9,
        },
    ]
}

let animationJalapeno = {
    name: 'jalapeno',
    pathFormat: 'img/Plants/jalapeno/[action]/jalapeno_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 8,
        },
        {
            name: 'attack',
            numberOfFrames: 8,
        },
    ]
}

let animationLilypad = {
    name: 'lilypad',
    pathFormat: 'img/Plants/lilypad/[action]/lilypad_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 3,
        },
    ]
}

let animationplantern = {
    name: 'plantern',
    pathFormat: 'img/Plants/plantern/[action]/plantern_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 19,
        },
    ]
}

let animationRepeater = {
    name: 'repeater',
    pathFormat: 'img/Plants/repeater/[action]/repeater_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 15,
        },
    ]
}

let animationSeashroom = {
    name: 'seashroom',
    pathFormat: 'img/Plants/seashroom/[action]/seashroom_[action]_[index].png',
    actions: [
        {
            name: 'sleep',
            numberOfFrames: 22,
        },
        {
            name: 'idle',
            numberOfFrames: 25,
        },
    ]
}

let animationSplitpea = {
    name: 'splitpea',
    pathFormat: 'img/Plants/splitpea/[action]/splitpea_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 14,
        },
    ]
}

let animationSquash = {
    name: 'squash',
    pathFormat: 'img/Plants/squash/[action]/squash_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 17,
        },
        {
            name: 'attack',
            numberOfFrames: 31,
        },
    ]
}

let animationSunshroom = {
    name: 'sunshroom',
    pathFormat: 'img/Plants/sunshroom/[action]/sunshroom_[action]_[index].png',
    actions: [
        {
            name: 'sleep',
            numberOfFrames: 14,
        },
        {
            name: 'idle',
            numberOfFrames: 10,
        },

        {
            name: 'stand',
            numberOfFrames: 10,
        },
    ]
}

let animationTallnut = {
    name: 'tallnut',
    pathFormat: 'img/Plants/tallnut/[action]/tallnut_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 14,
        },
        {
            name: 'cracked1',
            numberOfFrames: 13,
        },
        {
            name: 'cracked2',
            numberOfFrames: 12,
        },
    ]
}

let animationThreepeater = {
    name: 'threepeater',
    pathFormat: 'img/Plants/threepeater/[action]/threepeater_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 16,
        },
    ]
}

let animationSpikeweed = {
    name: 'spikeweed',
    pathFormat: 'img/Plants/spikeweed/[action]/spikeweed_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 19,
        },
    ]
}

let animationScaredyShroom = {
    name: 'ScaredyShroom',
    pathFormat: 'img/Plants/ScaredyShroom/[action]/ScaredyShroom_[action]_[index].png',
    actions: [
        {
            name: 'Sleep',
            numberOfFrames: 15,
        },
        {
            name: 'idle',
            numberOfFrames: 16,
        },
        {
            name: 'Cry',
            numberOfFrames: 10,
        },
    ]
}

let animationPumpkin = {
    name: 'pumpkin',
    pathFormat: 'img/Plants/pumpkin/[action]/pumpkin_[action]_[index].png',
    actions: [
        {
            name: 'head',
            numberOfFrames: 16,
        },
        {
            name: 'head1',
            numberOfFrames: 16,
        },
        {
            name: 'head2',
            numberOfFrames: 16,
        },
        {
            name: 'damage',
            numberOfFrames: 2,
        },
    ]
}

let animationTorchwood = {
    name: 'torchwood',
    pathFormat: 'img/Plants/torchwood/[action]/torchwood_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 9,
        },
        {
            name: 'fire',
            numberOfFrames: 1,
        },
    ]
}
let animationTwinsunflower = {
    name: 'twinsunflower',
    pathFormat: 'img/Plants/twinsunflower/[action]/twinsunflower_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 20,
        },
    ]
}

let animationWallnut = {
    name: 'wallnut',
    pathFormat: 'img/Plants/wallnut/[action]/wallnut_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 16,
        },
        {
            name: 'cracked1',
            numberOfFrames: 11,
        },
        {
            name: 'cracked2',
            numberOfFrames: 15,
        },
    ]
}

let animationCherry = {
    name: 'cherry',
    pathFormat: 'img/Plants/cherry/[action]/cherry_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 7,
        },
        {
            name: 'boom',
            numberOfFrames: 1,
        },
    ]
}

let animationPeashooter = {
    name: 'peashooter',
    pathFormat: 'img/Plants/peashooter/[action]/peashooter_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 13,
        },
    ]
}

let animationSnowPea = {
    name: 'snowpea',
    pathFormat: 'img/Plants/snowpea/[action]/snowpea_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 15,
        },
    ]
}

let animationPotatoMine = {
    name: 'potatomine',
    pathFormat: 'img/Plants/potatomine/[action]/potatomine_[action]_[index].png',
    actions: [
        {
            name: 'notready',
            numberOfFrames: 1,
        },
        {
            name: 'idle',
            numberOfFrames: 7,
        },
        {
            name: 'explosion',
            numberOfFrames: 1,
        },
    ]
}

let animationPuffShroom = {
    name: 'puffshroom',
    pathFormat: 'img/Plants/puffshroom/[action]/puffshroom_[action]_[index].png',
    actions: [
        {
            name: 'sleep',
            numberOfFrames: 16,
        },
        {
            name: 'fire',
            numberOfFrames: 13,
        },
    ]
}

let animationsunflower = {
    name: 'sunflower',
    pathFormat: 'img/Plants/sunflower/[action]/sunflower_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 17,
        },
    ]
}

let animationsun = {
    name: 'sun',
    pathFormat: 'img/Plants/sun/[action]/sun_[action]_[index].png',
    actions: [
        {
            name: 'idle',
            numberOfFrames: 21,
        },
    ]
}
// zombie
let animationZombie = {
    name: 'zombie',
    pathFormat: 'img/Zombies/zombie/[action]/zombie_[action]_[index].png',
    actions: [
        {
            name: 'walk3',
            numberOfFrames: 15,
        },

        {
            name: 'attack',
            numberOfFrames: 11,
        },
        {
            name: 'lostheadattack',
            numberOfFrames: 10,
        },
        {
            name: 'head',
            numberOfFrames: 11,
        },
        {
            name: 'lost_head',
            numberOfFrames: 11,
        },
        {
            name: 'die',
            numberOfFrames: 10,
        },
        {
            name: 'boomdie',
            numberOfFrames: 20,
        },
    ]
}

let animationBuckTheadZombie = {
    name: 'bhzombie',
    pathFormat: 'img/Zombies/bucketheadzombie/[action]/bucketheadzombie_[action]_[index].png',
    actions: [
        {
            name: 'walking',
            numberOfFrames: 15,
        },
        {
            name: 'attack',
            numberOfFrames: 11,
        },
        {
            name: 'boomdie',
            numberOfFrames: 20,
        },
    ]
}

let animationConeheadZombie = {
    name: 'chzombie',
    pathFormat: 'img/Zombies/coneheadzombie/[action]/coneheadzombie_[action]_[index].png',
    actions: [
        {
            name: 'walking',
            numberOfFrames: 21,
        },
        {
            name: 'attack',
            numberOfFrames: 11,
        },
        {
            name: 'boomdie',
            numberOfFrames: 20,
        },
    ]
}

let animationFlagZombie = {
    name: 'flagzombie',
    pathFormat: 'img/Zombies/flagzombie/[action]/flagzombie_[action]_[index].png',
    actions: [
        {
            name: 'walking',
            numberOfFrames: 12,
        },
        {
            name: 'attack',
            numberOfFrames: 11,
        },
        {
            name: 'losthead',
            numberOfFrames: 12,
        },
        {
            name: 'losthead_attack',
            numberOfFrames: 11,
        },
        {
            name: 'boomdie',
            numberOfFrames: 20,
        },

    ]
}


// peabullet

let peabulletShroom = {
    name: 'peabulletShroom',
    pathFormat: 'img/peabullet/[action]/peabullet_[action]_[index].png',
    actions: [
        {
            name: 'shroom',
            numberOfFrames: 5,
        },
    ]
}

let peabulletfire = {
    name: 'peabulletfire',
    pathFormat: 'img/peabullet/[action]/peabullet_[action]_[index].png',
    actions: [
        {
            name: 'fire',
            numberOfFrames: 2,
        },
    ]
}

let peabulletfire2 = {
    name: 'peabulletfire2',
    pathFormat: 'img/peabullet/[action]/peabullet_[action]_[index].png',
    actions: [
        {
            name: 'fire2',
            numberOfFrames: 2,
        },
    ]
}


let GuaAddAnimation = function (images, animation) {
    let a = animation
    let pathFormat = a.pathFormat
    let keyName = a.name
    //现在只支持两位数的frames,应该做成通用的
    for (let action of a.actions) {
        let name = action.name
        let numberOfFrames = action.numberOfFrames
        let p = pathFormat.replace('[action]', name).replace('[action]', name)

        for (let i = 0; i < numberOfFrames; i++) {
            // 1 01
            // 11 11

            let index = '0'.repeat(String(numberOfFrames).length - String(i).length) + String(i)
            // let name = `${p}${index}`
            // log('main index', index)
            let key = keyName + action.name + index
            let value = p.replace('[index]', index)
            images[key] = value
        }
    }

}

const images = {
    //background
    bg1: 'img/background/background1.jpg',
    bg2: 'img/background/background2.jpg',
    bg3: 'img/background/background3.jpg',
    bg4: 'img/background/background4.jpg',
    bg5: 'img/background/background5.jpg',
    bg6: 'img/background/background6boss.jpg',
    bgu1: 'img/background/background1unsodded.jpg',
    bgu2: 'img/background/background1unsodded1.jpg',
    bgu3: 'img/background/background1unsodded2.jpg',
    seedChooser_Background: 'img/background/SeedChooser_Background.png',
    choose_levelbg: 'img/background/choose_level.jpg',


    // peabullet
    peabullet1: 'img/peabullet/peabullet.gif',
    peabullet_hit: 'img/peabullet/peabullet_hit.png',
    peabullet_snow: 'img/peabullet/peabullet_snow.png',
    peabullet_shroomhit: 'img/peabullet/peabullet_shroomhit.png',


    // card
    card_Peashooter: 'img/cards/Peashooter.gif',
    card_Gralic: 'img/cards/Gralic.png',
    card_PotatoMine: 'img/cards/PotatoMine.gif',
    card_PumpkinHead: 'img/cards/PumpkinHead.gif',
    card_TwinSunflower: 'img/cards/TwinSunflower.gif',
    card_wallnut: 'img/cards/WallNut.gif',
    card_cherrybomb: 'img/cards/card_cherrybomb.png',
    card_chomper: 'img/cards/card_chomper.png',
    card_hypnoshroom: 'img/cards/card_hypnoshroom.png',
    card_iceshroom: 'img/cards/card_iceshroom.png',
    card_jalapeno: 'img/cards/card_jalapeno.png',
    card_repeaterpea: 'img/cards/card_repeaterpea.png',
    card_scaredyshroom: 'img/cards/card_scaredyshroom.png',
    card_snowpea: 'img/cards/card_snowpea.png',
    card_spikeweed: 'img/cards/card_spikeweed.png',
    card_squash: 'img/cards/card_squash.png',
    card_sunflower: 'img/cards/card_sunflower.png',
    card_sunshroom: 'img/cards/card_sunshroom.png',
    card_threepeashooter: 'img/cards/card_threepeashooter.png',


    // seed
    seed_blover: 'img/seed/Blover.png',
    seed_cactus: 'img/seed/Cactus.png',
    seed_cherrybomb: 'img/seed/CherryBomb.png',
    seed_chomper: 'img/seed/Chomper.png',
    seed_coffeeBean: 'img/seed/CoffeeBean.png',
    seed_doomShroom: 'img/seed/DoomShroom.png',
    seed_flowerPot: 'img/seed/FlowerPot.png',
    seed_fumeShroom: 'img/seed/FumeShroom.png',
    seed_garlic: 'img/seed/Garlic.png',
    seed_gatlingPea: 'img/seed/GatlingPea.png',
    seed_gloomShroom: 'img/seed/GloomShroom.png',
    seed_hypnoShroom: 'img/seed/HypnoShroom.png',
    seed_iceShroom: 'img/seed/IceShroom.png',
    seed_jalapeno: 'img/seed/Jalapeno.png',
    seed_lilyPad: 'img/seed/LilyPad.png',
    seed_peashooter: 'img/seed/Peashooter.png',
    seed_plantern: 'img/seed/Plantern.png',
    seed_potatoMine: 'img/seed/PotatoMine.png',
    seed_puffShroom: 'img/seed/PuffShroom.png',
    seed_pumpkinHead: 'img/seed/PumpkinHead.png',
    seed_repeater: 'img/seed/Repeater.png',
    seed_scaredyShroom: 'img/seed/ScaredyShroom.png',
    seed_seaShroom: 'img/seed/SeaShroom.png',
    seed_snowPea: 'img/seed/SnowPea.png',
    seed_spikerock: 'img/seed/Spikerock.png',
    seed_spikeweed: 'img/seed/Spikeweed.png',
    seed_splitPea: 'img/seed/SplitPea.png',
    seed_squash: 'img/seed/Squash.png',
    seed_starfruit: 'img/seed/Starfruit.png',
    seed_sunFlower: 'img/seed/SunFlower.png',
    seed_sunFlowerLimpid: 'img/seed/SunFlowerLimpid.png',
    seed_sunShroom: 'img/seed/SunShroom.png',
    seed_sunShroomLimpid: 'img/seed/SunShroomLimpid.png',
    seed_tallNut: 'img/seed/TallNut.png',
    seed_threepeater: 'img/seed/Threepeater.png',
    seed_torchwood: 'img/seed/Torchwood.png',
    seed_twinSunflower: 'img/seed/TwinSunflower.png',
    seed_wallNut: 'img/seed/WallNut.png',

    // other
    lawn_mower: 'img/LawnMower.png',

    // slogan
    slogan_prepare: 'img/slogan/prepare/slogan_prepare.png',
    slogan_large: 'img/slogan/large/slogan_large.png',
    slogan_final: 'img/slogan/final/slogan_final.png',

    // navigate
    Shovel_card: 'img/navigate/Shovel.png',
    ShovelBack_card: 'img/navigate/ShovelBack.png',
    score_back: 'img/navigate/score_back.png',
    card_back: 'img/navigate/card_back.png',
    sun: 'img/navigate/sun.png',
    sun_back: 'img/navigate/sun_back.png',

    // interface
    start_leave: 'img/interface/start_leave.png',
    start_over: 'img/interface/start_over.png',
    menu: 'img/interface/menu.png',
    main_menu: 'img/interface/MainMenu.png',
    sod1row: 'img/interface/sod1row.png',
    sod3row: 'img/interface/sod3row.png',
    adventure_0: 'img/interface/Adventure_0.png',
    GameLoose: 'img/interface/GameLoose.png',
    GameVictory: 'img/interface/GameVictory.png',
    mashed: 'img/interface/potatomine_mashed.png',
    squash_attack: 'img/Plants/squash/squash_attack.png',

}
//

let loadImage = function () {
    let animations = [animationZombie, animationPeashooter, animationSnowPea, animationPotatoMine, animationPuffShroom,
        animationsunflower, animationsun, animationBuckTheadZombie, animationConeheadZombie, animationFlagZombie,
        peabulletShroom, peabulletfire, peabulletfire2, animationCherry, animationWallnut, animationTwinsunflower,
        animationTorchwood, animationPumpkin, animationScaredyShroom, animationSpikeweed, animationThreepeater,
        animationTallnut, animationSunshroom, animationSquash, animationSplitpea, animationSeashroom, animationRepeater,
        animationplantern, animationLilypad, animationJalapeno, animationIceshroom, animationHypnoshroom, animationGloomshroom,
        animationGatlingpea, animationGarlic, animationFumeshroom, animationChomper,
    ]

    for (let a of animations) {
        GuaAddAnimation(images, a)
    }
}

loadImage()
